package toy.keli.edic.ui.main.components

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.FloatingActionButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import toy.keli.edic.R
import toy.keli.edic.ui.main.components.Display.mt
import toy.keli.edic.ui.main.extend.squareMinSize
import toy.keli.edic.util.ActivityRun


@Composable
fun ScanButton(
    onClick:()->Unit,
    color: Color = MaterialTheme.colorScheme.onBackground,
) {
    IconButton(onClick = {
        onClick()
    },
        modifier = Modifier
    ) {
        Icon(
            painter = painterResource(id = R.drawable.baseline_document_scanner),
            contentDescription = null,
            tint = color
        )
    }
}
@OptIn(ExperimentalFoundationApi::class)
@Composable
@Preview(showBackground = true)
fun RbButton(
    drawableId: Int = 0,
    text:String = "",
    color: Color = Color.Unspecified,
    containerColor:Color = MaterialTheme.colorScheme.onPrimary,
    onLongClick: () -> Unit = {},
    onClick: () -> Unit = {}
) {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(26.dp) // 添加全局边距
    ) {
        FloatingActionButton (
            onClick = onClick,
            modifier = Modifier
                .align(Alignment.BottomEnd)
                .alpha(0.8f)
                .combinedClickable(onClick={},onLongClick=onLongClick)
                .size(56.dp),
            shape = CircleShape, // 确保是正圆形
            containerColor = containerColor,
            elevation = FloatingActionButtonDefaults.elevation(
                defaultElevation = 6.dp,
                pressedElevation = 12.dp
            )
        ) {
            if(drawableId>0) {
                Icon(
                    painter = painterResource(id = drawableId),
                    contentDescription = null,
                    tint = color,
                    modifier = Modifier.size(24.dp) // 图标大小
                )
            }
            if(text.isNotEmpty()){
                Text(text)
            }
        }
    }
}

@Composable
fun SelectButton(text:String,onPopShow: (@Composable ()->Unit)?=null,onClick:()->Unit={}) {
    Text(text, modifier = Modifier.clickable{
            if(onPopShow!=null) {
                InfoDialog.show(onPopShow)
            }
            onClick()
        } .background(MaterialTheme.colorScheme.background)
        .padding(8.dp)
        .size(48.dp)
    )
}

@Composable
fun CloseButton(
    modifier: Modifier=Modifier,
    color:Color= Color.Red,
    onClick:()->Unit,

) {
    IconButton(onClick = {
        onClick()
    },
        modifier = modifier
    ) {
        Icon(
            painter = painterResource(id = R.drawable.delete),
            contentDescription = null,
            tint = color
        )
    }
}

/**
 * 视图按钮
 */
@Composable
@Preview
fun ViewButton(
    text:String="v",
    onButtonClick:()->Unit={},
) {
    Card(
        shape = RoundedCornerShape(18.dp),
        modifier = Modifier.clickable {
            onButtonClick()
        }.padding(horizontal = 5.dp)
    ) {
        Column(
            modifier = Modifier
                .background(MaterialTheme.colorScheme.onSurface)
                .padding(5.dp)
                .squareMinSize()
        ) {
            Text(
                text = mt(text),
                fontSize = 15.sp,
                color = MaterialTheme.colorScheme.surface,
                style = MaterialTheme.typography.titleSmall,
                modifier = Modifier.align(Alignment.CenterHorizontally)
            )
        }
    }
    Spacer(modifier = Modifier.width(5.dp))
}

/**
 * 搜索按钮
 */
@Composable
fun SearchButton(onClick:()->Unit){
    IconButton(
        onClick,
        modifier = Modifier
    ) {
        Icon(
            painter = painterResource(id = R.drawable.search),
            contentDescription = "搜索",
            tint = MaterialTheme.colorScheme.onBackground,
            modifier = Modifier
                .size(20.dp, 20.dp)
        )
    }
}

/**
 * 返回按钮
 */
@Composable
@Preview
fun GoBackButton(
    onClick:()->Unit={ ActivityRun.getAct().onBackPressed() },
) {
    IconButton(onClick = onClick, modifier = Modifier.size(30.dp)) {
        Icon(
            painter = painterResource(id = R.drawable.goback),
            contentDescription = null,
        )
    }
}

@Composable
fun CustomIconRadioButton(text:String,selectedResult:Int,onclick:()->Boolean={false}) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier.padding(10.dp)
            .clickable{
                onclick()
            }
    ) {
        // 使用 Box 和 Image 替代 RadioButton
        Box(
            modifier = Modifier
                .size(24.dp)
        ) {
            Image(
                painter = painterResource(
                    id = when(selectedResult) {
                        1 -> R.drawable.correct
                        2 -> R.drawable.wrong
                        else -> R.drawable.icon_mnkc_select
                    }
                ),
                contentDescription = null,
                modifier = Modifier.size(24.dp)
            )
        }

        Text(text, modifier = Modifier.padding(start = 8.dp))
    }
}